# coding:utf8

 
from x64dbgpy.pluginsdk import x64dbg
from x64dbgpy.pluginsdk import memory

module_base = 0
module_base = pluginsdk.GetMainModuleBase()





def parse_cfg_file(filename):

    g_num_count = 0
    # with open(filename, mode='r', encoding='utf-8') as file:
    # with open(filename, 'r', 'utf-8') as file:
    with open(filename, "r") as file:
        for line in file:
        
            #line = line.decode("utf-8")
            # 去除行尾的换行符并分割字段
            fields = line.strip().split('  ')
            
            try:
                # 解析地址和名称
                addr = int(fields[0], 16)  # 转换十六进制地址为整数
                name = str(fields[1])
                
                # 检查是否存在第三个字段（注释）
                # comment = fields[2] if len(fields) > 2 else ""
                
                print ("addr", addr, "name:", name)
                
                fun_addr = (module_base+addr)
                
                x64dbg.DbgSetLabelAt(fun_addr, name)

                if len(fields) > 2 :   
                    # comment = str(fields[2])
                    comment = fields[2]
                    print ("comment ", fields[2], True)
                    #comment = comment.encode('gbk', 'ignore')
                    #print ("comment.encode ", comment, True)
                    x64dbg.Comment_Set(fun_addr, comment)
                    
                g_num_count +=1 
                    
                print ("numcount", g_num_count, "\n")
                
            except ValueError:
                print("Error parsing line:", line)
                # 在这里添加任何需要的错误处理逻辑，比如跳过当前行或记录错误信息
                continue
                
           
# 调用函数并传入文件名
parse_cfg_file("C:/Users/David/Desktop/x64dbgpy/ALLhookExport.txt")
print ("import over")